﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DanielsGameLibrary
{

    public class Math2D
    {
        public static float DistanceHscor(Vector2 a, Vector2 b)
        {
            Vector2 c;
            if (a.X > b.X)
                c.X = a.X - b.X;
            else
                c.X = b.X - a.X;

            if (a.Y > b.Y)
                c.Y = a.Y - b.Y;
            else
                c.Y = b.Y - a.Y;

            return c.X + c.Y;
        }

        public static double Distance(Vector2 a, Vector2 b)
        {
            Vector2 c = a - b;
            return Math.Sqrt(c.X + c.Y);

        }

        public static int getSignIdentety(float input)
        {
            if (input > 0)
                return 1;
            if (input < 0)
                return -1;
            else
                return 0;
        }

        public static float abs(float input)
        {
            if (input > 0)
                return input;
            if (input < 0)
                return input * -1;
            else
                return 0;
        }


        public static float normalize(float input, float max)
        {
            if (abs(input) <= max)
                return input;
            else
                return max * getSignIdentety(input);
        }
    }

    public class Math3D
    {
        public static Vector3 RandomVector3()
        {
            Random rand = new Random();

            Vector3 angels = new Vector3();
            angels.X = rand.Next(360) - 180;
            angels.Y = rand.Next(360) - 180;
            angels.Z = rand.Next(20000) - 10000;

            Vector3 result = new Vector3();
            result.X = (float)((angels.Z * Math.Sin(MathHelper.ToRadians(angels.Y))) * Math.Sin(MathHelper.ToRadians(angels.X)));
            result.Y = (float)(angels.Z * Math.Sin(MathHelper.ToRadians(angels.Y)));
            result.Z = (float)((angels.Z * Math.Sin(MathHelper.ToRadians(angels.Y))) * Math.Cos(MathHelper.ToRadians(angels.X)));

            result.Normalize();

            return result;
        }

        public static Vector4 CreatePlane(float height, Vector3 planeNormalDirection, bool clipSide)
        {
            planeNormalDirection.Normalize();
            Vector4 planeCoeffs = new Vector4(planeNormalDirection, -height);
            if (clipSide)
                planeCoeffs *= -1;


            //Plane finalPlane = new Plane(planeCoeffs);

            return planeCoeffs;
        }
    }
}